package com.example.bee_work_3.fragment.home;

import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bee_work_3.webc_ontent.IntentCallBack;
import com.example.bee_work_3.fragment.ListItem;
import com.example.bee_work_3.MainActivity;
import com.example.bee_work_3.R;

import java.util.List;

public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    static int position = 0;
    static HeadViewHolder head;

    private int mResourceId;
    private List<ListItem> mList;
    private IntentCallBack mCallBack;
    private PicturePagerAdapter mPicturePagerAdapter;


    public ItemAdapter(int textViewResourceId, List<ListItem> list, PicturePagerAdapter picturePagerAdapter
            , IntentCallBack callBack ) {
        this.mList = list;
        this.mCallBack = callBack;
        this.mResourceId = textViewResourceId;
        this.mPicturePagerAdapter = picturePagerAdapter;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == 1) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(mResourceId, null);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }
        View headView = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.header_picture, null);
        HeadViewHolder headViewHolder = new HeadViewHolder(headView);
        head = headViewHolder;
        return headViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof ViewHolder) {
            ViewHolder holder = (ViewHolder) viewHolder;
            final ListItem temp = mList.get(i);
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCallBack.onIntent(temp);
                }
            });
            if (temp.getText() != null) {
                holder.content.setText(temp.getText());
            }
            String detailText = temp.getmAuthor()+" "+temp.getmDate();
            if (!detailText.equals(" ")){
                holder.detail.setText(detailText);
            }
            return;
        }
        final HeadViewHolder headViewHolder = (HeadViewHolder) viewHolder;
        headViewHolder.viewPager.setAdapter(mPicturePagerAdapter);
        headViewHolder.viewPager.setVisibility(View.VISIBLE);
        headViewHolder.index.setVisibility(View.VISIBLE);
        headViewHolder.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                position = i;
            }

            @Override
            public void onPageSelected(final int i){
                headViewHolder.index.setText((i + 1) + "/" + mPicturePagerAdapter.getCount());
            }

            @Override
            public void onPageScrollStateChanged(int i) {
                if (position == 0) {
                    MainActivity.swipeLayout.setSwipeEnabled(true);
                } else {
                    MainActivity.swipeLayout.setSwipeEnabled(false);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView content,detail;
        ImageView image;
        RelativeLayout cardView;

        public ViewHolder(View view) {
            super(view);
            content = view.findViewById(R.id.content);
            detail = view.findViewById(R.id.detail);
            image = view.findViewById(R.id.image);
            cardView = view.findViewById(R.id.item_cardView);
        }
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {
        ViewPager viewPager;
        TextView index;

        public HeadViewHolder(View view) {
            super(view);
            viewPager = view.findViewById(R.id.card_viewPager);
            index = view.findViewById(R.id.index);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return 0;
        return 1;
    }
}